package cn.wzvtcsoft.sys.security.rbac.controller;

import cn.wzvtcsoft.common.vo.Result;
import cn.wzvtcsoft.sys.entity.User;
import cn.wzvtcsoft.sys.security.BosUserDetails;
import cn.wzvtcsoft.sys.security.rbac.service.SecurityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author swxu_2005@163.com
 */
@RestController
public class SecurityController {

  @Autowired
  private SecurityServiceImpl securityService;

  /**
   * 获取当前登陆用户的信息。与vue-element-admin前端对接所需
   */
  @GetMapping("/user/info")
//  @Cacheable(cacheNames = "userInfo", key = "#bosUserDetails.user.id")
  public Result getUserInfo(@AuthenticationPrincipal BosUserDetails bosUserDetails) {
    User user = bosUserDetails.getUser();
    Set<String> authorizedMenus = securityService.getAuthorizedMenus(user.getId());

    return Result.ok()
        .put("roles", authorizedMenus)
        .put("name", user.getNumber())
        .put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif")
        .put("introduction", "");
  }
}
